package com.lanlian.cayman.adapter;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lanlian.commonlib.bean.db.DBMarketS;
import com.lanlian.commonlib.dao.DBManager;
import com.lanlian.commonlib.data.GoodsBean;
import com.lanlian.cayman.R;
import com.lanlian.cayman.bean.MyContractBean;
import com.lanlian.cayman.fragment.MarketFragment;
import com.lanlian.cayman.utils.MResUtil;
import com.lanlian.cayman.utils.NumberManager;
import com.lanlian.utillib.NumberUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 交易适配器
 */
public class MarketAdapter extends BaseQuickAdapter<MyContractBean, BaseViewHolder> {

    private List<MyContractBean> mList;

    public MarketAdapter(int layoutResId, @Nullable List<MyContractBean> data) {
        super(layoutResId, data);
        this.mList = data;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder helper, int position, @NonNull List<Object> payloads) {
        if (payloads.isEmpty()) {
            onBindViewHolder(helper, position);
        } else {
            MyContractBean item = mList.get(position);
            if (isZiXuan(item.id)) {
                helper.setImageResource(R.id.iv, R.mipmap.quotation_icon_cutdown);
            } else {
                helper.setImageResource(R.id.iv, R.mipmap.quotation_icon_addto);
            }
            GoodsBean goodsBean = MarketFragment.getRespMarket().getGoods(item.id);
            int count = 2;
            if (goodsBean != null) {
                count = NumberManager.getNumberDecimalDigits(goodsBean.minChange);
            }
            if (TextUtils.isEmpty(item.last)) {
                helper.setText(R.id.tv_last, "- -");
                helper.setText(R.id.tv_chg, "- -");
                helper.setText(R.id.tv_change, "- -");
                helper.setBackgroundRes(R.id.tv_chg, MResUtil.getPingBack());
                helper.setTextColor(R.id.tv_change, MResUtil.getPingColor());
            } else {
                helper.setText(R.id.tv_last, String.valueOf(NumberUtils.round(item.last, count, BigDecimal.ROUND_DOWN)));
                String chg = NumberManager.getStringNoZero("1", item.chg, 2);
                String change = NumberManager.getStringNoZero("1", item.change, count);
                helper.setText(R.id.tv_chg, chg + "%");
                helper.setText(R.id.tv_change, change);
                if (Double.parseDouble(chg) > 0) {
                    helper.setBackgroundRes(R.id.tv_chg, MResUtil.getUpBack());
                    helper.setTextColor(R.id.tv_change, MResUtil.getUpColor());
                } else if (Double.parseDouble(chg) == 0) {
                    helper.setBackgroundRes(R.id.tv_chg, MResUtil.getPingBack());
                    helper.setTextColor(R.id.tv_change, MResUtil.getPingColor());
                } else {
                    helper.setBackgroundRes(R.id.tv_chg, MResUtil.getDownBack());
                    helper.setTextColor(R.id.tv_change, MResUtil.getDownColor());
                }
            }
        }
    }

    @Override
    protected void convert(BaseViewHolder helper, MyContractBean item) {
        if (helper.getLayoutPosition() % 2 == 0) {
            helper.setBackgroundColor(R.id.lay_position, 0xffF2F6F9);
        } else {
            helper.setBackgroundColor(R.id.lay_position, 0xffFFFFFF);
        }
        helper.setText(R.id.tv_treaty, item.name);
        if (isZiXuan(item.id)) {
            helper.setImageResource(R.id.iv, R.mipmap.quotation_icon_cutdown);
        } else {
            helper.setImageResource(R.id.iv, R.mipmap.quotation_icon_addto);
        }
        helper.addOnClickListener(R.id.ll_iv);

        GoodsBean goodsBean = MarketFragment.getRespMarket().getGoods(item.id);
        int count = 2;
        if (goodsBean != null) {
            count = NumberManager.getNumberDecimalDigits(goodsBean.minChange);
        }
        if (TextUtils.isEmpty(item.last)) {
            helper.setText(R.id.tv_last, "- -");
            helper.setText(R.id.tv_chg, "- -");
            helper.setText(R.id.tv_change, "- -");
            helper.setBackgroundRes(R.id.tv_chg, MResUtil.getPingBack());
            helper.setTextColor(R.id.tv_change, MResUtil.getPingColor());
        } else {
            helper.setText(R.id.tv_last, String.valueOf(NumberUtils.round(item.last, count, BigDecimal.ROUND_DOWN)));
            String chg = NumberManager.getStringNoZero("1", item.chg, 2);
            String change = NumberManager.getStringNoZero("1", item.change, count);
            helper.setText(R.id.tv_chg, chg + "%");
            helper.setText(R.id.tv_change, change);
            if (Double.parseDouble(chg) > 0) {
                helper.setBackgroundRes(R.id.tv_chg, MResUtil.getUpBack());
                helper.setTextColor(R.id.tv_change, MResUtil.getUpColor());
            } else if (Double.parseDouble(chg) == 0) {
                helper.setBackgroundRes(R.id.tv_chg, MResUtil.getPingBack());
                helper.setTextColor(R.id.tv_change, MResUtil.getPingColor());
            } else {
                helper.setBackgroundRes(R.id.tv_chg, MResUtil.getDownBack());
                helper.setTextColor(R.id.tv_change, MResUtil.getDownColor());
            }
        }

    }

    private boolean isZiXuan(String id) {
        List<DBMarketS> list = DBManager.getInstance().getMarketS();
        if (list != null) {
            for (DBMarketS dbMarketS : list) {
                if (id.equals(dbMarketS.getId())) {
                    return true;
                }
            }
        }
        return false;
    }

}
